package com.situ.feemanagement.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.feemanagement.mapper.MedicineMapper;
import com.situ.feemanagement.mapper.RestockingMapper;
import com.situ.feemanagement.pojo.Medicine;
import com.situ.feemanagement.pojo.Restocking;
import com.situ.feemanagement.pojo.query.RestockingQuery;
import com.situ.feemanagement.pojo.vo.RestockingVO;
import com.situ.feemanagement.service.IRestockingService;
import com.situ.feemanagement.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RestockingServiceImpl implements IRestockingService {

    @Autowired
    private RestockingMapper restockingMapper;
    @Autowired
    private MedicineMapper medicineMapper;

    @Override
    public PageResult<RestockingVO> selectByPage(RestockingQuery restockingQuery) {
        PageHelper.startPage(restockingQuery.getPage(), restockingQuery.getLimit());
        List<RestockingVO> list = restockingMapper.selectByPage(restockingQuery);
        PageInfo<RestockingVO> pageInfo = new PageInfo<>(list);
        int totalCount = (int) pageInfo.getTotal();
        return new PageResult<>(0, "", totalCount, list);
    }

    @Override
    public void deleteById(int id) {
        Restocking restocking = restockingMapper.selectById(id);
        Medicine medicine = medicineMapper.selectById(restocking.getMedicineId());
        int inventory = medicine.getInventory() - restocking.getQuantity();
        medicineMapper.updateInventory(restocking.getMedicineId(),inventory);
        restockingMapper.deleteById(id);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            Restocking restocking = restockingMapper.selectById(Integer.parseInt(id));
            Medicine medicine = medicineMapper.selectById(restocking.getMedicineId());
            int inventory = medicine.getInventory() - restocking.getQuantity();
            medicineMapper.updateInventory(restocking.getMedicineId(),inventory);
            restockingMapper.deleteById(Integer.parseInt(id));
        }
    }

    @Override
    public void add(Restocking restocking) {
        restockingMapper.add(restocking);
        System.out.println(restocking.getMedicineId());
        Medicine medicine = medicineMapper.selectById(restocking.getMedicineId());
        int inventory = medicine.getInventory() + restocking.getQuantity();
        medicineMapper.updateInventory(restocking.getMedicineId(),inventory);
    }

    @Override
    public Restocking selectById(int id) {
        return restockingMapper.selectById(id);
    }

    @Override
    public void update(Restocking restocking) {
        Restocking oldRestocking = restockingMapper.selectById(restocking.getId());
        Medicine oldMedicine = medicineMapper.selectById(oldRestocking.getMedicineId());
        int oldInventory = oldMedicine.getInventory() - oldRestocking.getQuantity();
        medicineMapper.updateInventory(oldMedicine.getId(),oldInventory);
        Medicine newMedicine = medicineMapper.selectById(oldRestocking.getMedicineId());
        int newInventory = newMedicine.getInventory() + restocking.getQuantity();
        medicineMapper.updateInventory(newMedicine.getId(),newInventory);
        restockingMapper.update(restocking);
    }
}
